class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<int> f(n + 1, INT_MAX);

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                f[j + 1] = (i == 0 && j == 0) ? grid[i][j] : min(f[j], f[j + 1]) + grid[i][j];
            }
        }
        return f[n];
    }
};